<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>BadgerDB: bufmgr/src/file.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">BadgerDB
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">bufmgr/src/file.h</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#pragma once</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;fstream&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;map&gt;</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &lt;memory&gt;</span>
<a name="l00014"></a>00014 
<a name="l00015"></a>00015 <span class="preprocessor">#include &quot;page.h&quot;</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 <span class="keyword">namespace </span>badgerdb {
<a name="l00018"></a>00018 
<a name="l00019"></a>00019 <span class="keyword">class </span>FileIterator;
<a name="l00020"></a>00020 
<a name="l00024"></a><a class="code" href="structbadgerdb_1_1_file_header.html">00024</a> <span class="keyword">struct </span><a class="code" href="structbadgerdb_1_1_file_header.html" title="Header metadata for files on disk which contain pages.">FileHeader</a> {
<a name="l00028"></a><a class="code" href="structbadgerdb_1_1_file_header.html#aa1cfc5220c6795868c8301cec298c8a6">00028</a>   <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> <a class="code" href="structbadgerdb_1_1_file_header.html#aa1cfc5220c6795868c8301cec298c8a6">num_pages</a>;
<a name="l00029"></a>00029 
<a name="l00033"></a><a class="code" href="structbadgerdb_1_1_file_header.html#ad006b5b9d02b8c20e7ad5226680779c6">00033</a>   <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> <a class="code" href="structbadgerdb_1_1_file_header.html#ad006b5b9d02b8c20e7ad5226680779c6">first_used_page</a>;
<a name="l00034"></a>00034 
<a name="l00038"></a><a class="code" href="structbadgerdb_1_1_file_header.html#a07ac20f98d6aced3eb5358e0d4830abc">00038</a>   <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> <a class="code" href="structbadgerdb_1_1_file_header.html#a07ac20f98d6aced3eb5358e0d4830abc">num_free_pages</a>;
<a name="l00039"></a>00039 
<a name="l00043"></a><a class="code" href="structbadgerdb_1_1_file_header.html#aa86917d8554a42318c795471f32ad006">00043</a>   <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> <a class="code" href="structbadgerdb_1_1_file_header.html#aa86917d8554a42318c795471f32ad006">first_free_page</a>;
<a name="l00044"></a>00044 
<a name="l00051"></a><a class="code" href="structbadgerdb_1_1_file_header.html#ae6439483acdb0eee52654c9c401793ca">00051</a>   <span class="keywordtype">bool</span> <a class="code" href="structbadgerdb_1_1_file_header.html#ae6439483acdb0eee52654c9c401793ca">operator==</a>(<span class="keyword">const</span> <a class="code" href="structbadgerdb_1_1_file_header.html" title="Header metadata for files on disk which contain pages.">FileHeader</a>&amp; rhs)<span class="keyword"> const </span>{
<a name="l00052"></a>00052     <span class="keywordflow">return</span> <a class="code" href="structbadgerdb_1_1_file_header.html#aa1cfc5220c6795868c8301cec298c8a6">num_pages</a> == rhs.<a class="code" href="structbadgerdb_1_1_file_header.html#aa1cfc5220c6795868c8301cec298c8a6">num_pages</a> &amp;&amp;
<a name="l00053"></a>00053         <a class="code" href="structbadgerdb_1_1_file_header.html#a07ac20f98d6aced3eb5358e0d4830abc">num_free_pages</a> == rhs.<a class="code" href="structbadgerdb_1_1_file_header.html#a07ac20f98d6aced3eb5358e0d4830abc">num_free_pages</a> &amp;&amp;
<a name="l00054"></a>00054         <a class="code" href="structbadgerdb_1_1_file_header.html#ad006b5b9d02b8c20e7ad5226680779c6">first_used_page</a> == rhs.<a class="code" href="structbadgerdb_1_1_file_header.html#ad006b5b9d02b8c20e7ad5226680779c6">first_used_page</a> &amp;&amp;
<a name="l00055"></a>00055         <a class="code" href="structbadgerdb_1_1_file_header.html#aa86917d8554a42318c795471f32ad006">first_free_page</a> == rhs.<a class="code" href="structbadgerdb_1_1_file_header.html#aa86917d8554a42318c795471f32ad006">first_free_page</a>;
<a name="l00056"></a>00056   }
<a name="l00057"></a>00057 };
<a name="l00058"></a>00058 
<a name="l00073"></a><a class="code" href="classbadgerdb_1_1_file.html">00073</a> <span class="keyword">class </span><a class="code" href="classbadgerdb_1_1_file.html" title="Class which represents a file in the filesystem containing database pages.">File</a> {
<a name="l00074"></a>00074  <span class="keyword">public</span>:
<a name="l00081"></a>00081   <span class="keyword">static</span> <a class="code" href="classbadgerdb_1_1_file.html" title="Class which represents a file in the filesystem containing database pages.">File</a> <a class="code" href="classbadgerdb_1_1_file.html#a1fb708b45103a606f189850d6bf83a0c">create</a>(<span class="keyword">const</span> std::string&amp; <a class="code" href="classbadgerdb_1_1_file.html#ac61ce3c2233fd1f7e2e2a8a5a7640936">filename</a>);
<a name="l00082"></a>00082 
<a name="l00093"></a>00093   <span class="keyword">static</span> <a class="code" href="classbadgerdb_1_1_file.html" title="Class which represents a file in the filesystem containing database pages.">File</a> <a class="code" href="classbadgerdb_1_1_file.html#a8462afdbd250c0a483ddfbde144c8732">open</a>(<span class="keyword">const</span> std::string&amp; filename);
<a name="l00094"></a>00094 
<a name="l00102"></a>00102   <span class="keyword">static</span> <span class="keywordtype">void</span> <span class="keyword">remove</span>(<span class="keyword">const</span> std::string&amp; <a class="code" href="classbadgerdb_1_1_file.html#ac61ce3c2233fd1f7e2e2a8a5a7640936">filename</a>);
<a name="l00103"></a>00103 
<a name="l00109"></a>00109   <span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classbadgerdb_1_1_file.html#a64836156a9bb5f81d2c2c4e6f3ada24d">isOpen</a>(<span class="keyword">const</span> std::string&amp; filename);
<a name="l00110"></a>00110 
<a name="l00111"></a>00111 
<a name="l00117"></a>00117   <span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classbadgerdb_1_1_file.html#a864d59b12302c26b14967bd1d3e520bd">exists</a>(<span class="keyword">const</span> std::string&amp; filename);
<a name="l00118"></a>00118 
<a name="l00125"></a>00125   <a class="code" href="classbadgerdb_1_1_file.html#a600baaf77d18f39e51ae7473eca633c4">File</a>(<span class="keyword">const</span> <a class="code" href="classbadgerdb_1_1_file.html" title="Class which represents a file in the filesystem containing database pages.">File</a>&amp; other);
<a name="l00126"></a>00126 
<a name="l00133"></a>00133   <a class="code" href="classbadgerdb_1_1_file.html" title="Class which represents a file in the filesystem containing database pages.">File</a>&amp; <a class="code" href="classbadgerdb_1_1_file.html#ac403c631aec085e9f12992f260a57155">operator=</a>(<span class="keyword">const</span> <a class="code" href="classbadgerdb_1_1_file.html" title="Class which represents a file in the filesystem containing database pages.">File</a>&amp; rhs);
<a name="l00134"></a>00134 
<a name="l00139"></a>00139   <a class="code" href="classbadgerdb_1_1_file.html#a97fd6e3ae8dd11be883289b69d560287">~File</a>();
<a name="l00140"></a>00140 
<a name="l00146"></a>00146   <a class="code" href="classbadgerdb_1_1_page.html" title="Class which represents a fixed-size database page containing records.">Page</a> <a class="code" href="classbadgerdb_1_1_file.html#a7d0e047bcc8dc4cee36aac5b2060bbe3">allocatePage</a>();
<a name="l00147"></a>00147 
<a name="l00156"></a>00156   <a class="code" href="classbadgerdb_1_1_page.html" title="Class which represents a fixed-size database page containing records.">Page</a> <a class="code" href="classbadgerdb_1_1_file.html#aba4762b533499b7b7cc0774192ead27c">readPage</a>(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> page_number) <span class="keyword">const</span>;
<a name="l00157"></a>00157 
<a name="l00165"></a>00165   <span class="keywordtype">void</span> <a class="code" href="classbadgerdb_1_1_file.html#a9a1b3cc43c4631bde58c1c4f670e1036">writePage</a>(<span class="keyword">const</span> <a class="code" href="classbadgerdb_1_1_page.html" title="Class which represents a fixed-size database page containing records.">Page</a>&amp; new_page);
<a name="l00166"></a>00166 
<a name="l00172"></a>00172   <span class="keywordtype">void</span> <a class="code" href="classbadgerdb_1_1_file.html#a829a68ccfbeb0c9f66f3a2db7e0b99fe">deletePage</a>(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> page_number);
<a name="l00173"></a>00173 
<a name="l00179"></a><a class="code" href="classbadgerdb_1_1_file.html#ac61ce3c2233fd1f7e2e2a8a5a7640936">00179</a>   <span class="keyword">const</span> std::string&amp; <a class="code" href="classbadgerdb_1_1_file.html#ac61ce3c2233fd1f7e2e2a8a5a7640936">filename</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> filename_; }
<a name="l00180"></a>00180 
<a name="l00186"></a>00186   <a class="code" href="classbadgerdb_1_1_file_iterator.html" title="Iterator for iterating over the pages in a file.">FileIterator</a> <a class="code" href="classbadgerdb_1_1_file.html#a32e94f392eb3740dd9eb9101b9e09d47">begin</a>();
<a name="l00187"></a>00187 
<a name="l00194"></a>00194   <a class="code" href="classbadgerdb_1_1_file_iterator.html" title="Iterator for iterating over the pages in a file.">FileIterator</a> <a class="code" href="classbadgerdb_1_1_file.html#a01e224676a67634f21d53fc4189b8f11">end</a>();
<a name="l00195"></a>00195 
<a name="l00196"></a>00196  <span class="keyword">private</span>:
<a name="l00204"></a>00204   <span class="keyword">static</span> std::streampos pagePosition(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> page_number) {
<a name="l00205"></a>00205     <span class="keywordflow">return</span> <span class="keyword">sizeof</span>(<a class="code" href="structbadgerdb_1_1_file_header.html" title="Header metadata for files on disk which contain pages.">FileHeader</a>) + ((page_number - 1) * <a class="code" href="classbadgerdb_1_1_page.html#ae92d52803502854f97309bae2c14ea55">Page::SIZE</a>);
<a name="l00206"></a>00206   }
<a name="l00207"></a>00207 
<a name="l00222"></a>00222   <a class="code" href="classbadgerdb_1_1_file.html#a600baaf77d18f39e51ae7473eca633c4">File</a>(<span class="keyword">const</span> std::string&amp; name, <span class="keyword">const</span> <span class="keywordtype">bool</span> create_new);
<a name="l00223"></a>00223 
<a name="l00235"></a>00235   <span class="keywordtype">void</span> openIfNeeded(<span class="keyword">const</span> <span class="keywordtype">bool</span> create_new);
<a name="l00236"></a>00236 
<a name="l00242"></a>00242   <span class="keywordtype">void</span> close();
<a name="l00243"></a>00243 
<a name="l00257"></a>00257   Page <a class="code" href="classbadgerdb_1_1_file.html#aba4762b533499b7b7cc0774192ead27c">readPage</a>(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> page_number, <span class="keyword">const</span> <span class="keywordtype">bool</span> allow_free) <span class="keyword">const</span>;
<a name="l00258"></a>00258 
<a name="l00267"></a>00267   <span class="keywordtype">void</span> <a class="code" href="classbadgerdb_1_1_file.html#a9a1b3cc43c4631bde58c1c4f670e1036">writePage</a>(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> page_number, <span class="keyword">const</span> Page&amp; new_page);
<a name="l00268"></a>00268 
<a name="l00278"></a>00278   <span class="keywordtype">void</span> <a class="code" href="classbadgerdb_1_1_file.html#a9a1b3cc43c4631bde58c1c4f670e1036">writePage</a>(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> page_number, <span class="keyword">const</span> PageHeader&amp; header,
<a name="l00279"></a>00279                  <span class="keyword">const</span> Page&amp; new_page);
<a name="l00280"></a>00280 
<a name="l00286"></a>00286   FileHeader readHeader() <span class="keyword">const</span>;
<a name="l00287"></a>00287 
<a name="l00293"></a>00293   <span class="keywordtype">void</span> writeHeader(<span class="keyword">const</span> FileHeader&amp; header);
<a name="l00294"></a>00294 
<a name="l00302"></a>00302   PageHeader readPageHeader(<span class="keyword">const</span> <a class="code" href="namespacebadgerdb.html#a1f49e404293bf4240756b89b53b1587a" title="Identifier for a page in a file.">PageId</a> page_number) <span class="keyword">const</span>;
<a name="l00303"></a>00303 
<a name="l00304"></a>00304   <span class="keyword">typedef</span> std::map&lt;std::string,
<a name="l00305"></a>00305                    std::shared_ptr&lt;std::fstream&gt; &gt; StreamMap;
<a name="l00306"></a>00306   <span class="keyword">typedef</span> std::map&lt;std::string, int&gt; CountMap;
<a name="l00307"></a>00307 
<a name="l00311"></a>00311   <span class="keyword">static</span> StreamMap open_streams_;
<a name="l00312"></a>00312 
<a name="l00316"></a>00316   <span class="keyword">static</span> CountMap open_counts_;
<a name="l00317"></a>00317 
<a name="l00321"></a>00321   std::string filename_;
<a name="l00322"></a>00322 
<a name="l00326"></a>00326   std::shared_ptr&lt;std::fstream&gt; stream_;
<a name="l00327"></a>00327 
<a name="l00328"></a>00328   <span class="keyword">friend</span> <span class="keyword">class </span>FileIterator;
<a name="l00329"></a>00329   <span class="keyword">friend</span> <span class="keyword">class </span>FileTest;
<a name="l00330"></a>00330 };
<a name="l00331"></a>00331 
<a name="l00332"></a>00332 }
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Friends</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Wed Jan 30 2013 14:17:53 for BadgerDB by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
